package com.mygodp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.mygodp.dto.Result;
import com.mygodp.entity.ShopType;
import com.mygodp.mapper.ShopTypeMapper;
import com.mygodp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mygodp.utils.RedisConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryList() {
        String key= "CACHE_SHOP_TYPE";
        String typeJson = stringRedisTemplate.opsForValue().get(key);

        if (StrUtil.isNotBlank(typeJson)) {
            List<ShopType> shopType = JSONUtil.toList(typeJson, ShopType.class);
            return Result.ok(shopType);
        }

        List<ShopType> shopType = query().orderByAsc("sort").list();
        if (shopType==null){
            return Result.fail("分类不存在！");
        }
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shopType), RedisConstants.CACHE_SHOP_TTL, TimeUnit.MINUTES);
        return Result.ok(shopType);
    }
}
